package web;

import service.ArticlesService;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author fire'ge
 */
@WebServlet("/addArticleServlet")
public class AddArticleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding( "utf-8" );
        response.setContentType( "text/html;charset=utf-8" );
        String topic = request.getParameter("topic");
        String text = request.getParameter("text");
        User user = (User)request.getSession().getAttribute("user");
        String name = user.getName();
        Boolean isUpdate = false;
        try {
            isUpdate = ArticlesService.addArticle(topic, text, name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (isUpdate){
            request.setAttribute("msg","提交成功，请返回");
        }
        else {
            request.setAttribute("msg","提交失败，请返回尝试重新提交，或联系管理员");

        }
        request.getRequestDispatcher("/jsp/msg.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
